app.controller('BusPriceScheduleModalCtrl', function ($scope, $filter, $modalInstance, toaster, param, ajax, $modal) {

	$scope.dplist = [];

	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	$scope.tplId = $scope.param.tplId;
	$scope.routeId = $scope.param.routeId;

	$scope.timeRange = {};
	if ($scope.param.type == "update") {
		$scope.timeRange.startTime = $filter("date")($scope.param.data.availFrom, "yyyy-MM-dd");
		$scope.timeRange.endTime = $filter("date")($scope.param.data.availTo, "yyyy-MM-dd");
	} else {
		$scope.param.data.price = 0;
		$scope.param.data.lowest = 0;
		$scope.param.data.parameter = 0;
	}

	var inserter = new Req.POST({ resource: $scope.resource });

	//获取站点列表
	$scope.loadStationList = function () {
		ajax.call(new Req.GET({
			resource: 'route/bs/' + $scope.routeId + '/station'
		})).then(function (result) {
			$scope.stationList = result.result;
		}, function () { });
	};
	$scope.loadStationList();

	$scope.priceList = [];

	$scope.calc = function () {
		if ($scope.myForm.$valid) {
			if ($scope.dplist.selected == undefined) {
				toaster.pop('warning', '提示', '请选择类型！');
				return;
			}
			$scope.param.data.type = $scope.dplist.selected.id;
			inserter.fields = $scope.param.data;
			inserter.resource = 'price/bs/' + $scope.param.routeId + '/calc';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.priceList = result.result;
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '计算价格失败。');
			});
		}
	};

	$scope.loaddplist = function () {
		$scope.dplist = [
			{ id: 0, name: "无折扣" },
			{ id: 1, name: "等额递减" },
			{ id: 2, name: "等差递减" }
		];
		if ($scope.param.data && $scope.param.data.type >= 0) {
			var id = $scope.param.data.type;
			$scope.dplist.selected = $scope.dplist[id];
		} else {
			$scope.dplist.selected = $scope.dplist[0];
		}
	};
	$scope.loaddplist();

	$scope.syncShiftModal = function (shifts) {
		return $modal.open({
			templateUrl: 'template/bus/priceSchedule/syncShift.html?v=01be5186b4',
			controller: 'BusPriceSyncShiftsCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"list": shifts
					};
				}
			}
		});
	};

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if (!$scope.dplist.selected) {
				toaster.pop('warning', '提示', '请选择类型！');
				return;
			} else {
				$scope.param.data.type = $scope.dplist.selected.id;
			}
			if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
				toaster.pop('warning', '提示', '请选择价格计划有效时间！');
				return;
			}
			if (Number($scope.param.data.price) <= 0 || Number($scope.param.data.lowest) > Number($scope.param.data.price)) {
				toaster.pop('warning', '提示', '基础票价必须大于0且最低票价不得大于基础票价！');
				return;
			}
			$scope.param.data.templateId = $scope.tplId;
			$scope.param.data.availFrom = new Date($scope.timeRange.startTime);
			$scope.param.data.availTo = new Date($scope.timeRange.endTime);
			if ($scope.param.data.type == 0) {
				$scope.param.data.lowest = $scope.param.data.price;
				$scope.param.data.parameter = 0;
			}

			ajax.call(new Req.POST({
				fields: $scope.param.data,
				resource: 'price/bs/schedule/shifts'
			})).then(function (result) {
				if (result.code == 200 && result.result.length > 0) {
					$scope.syncShiftModal(result.result).result.then(function (param) {
						if (param && param.length > 0) $scope.param.ids = param;
						$modalInstance.close($scope.param);
					}, function () {
						$modalInstance.close($scope.param);
					});
				} else {
					$modalInstance.close($scope.param);
				}
			}, function () {
				$modalInstance.close($scope.param);
			});
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};

	$scope.noDiscount = false;
	$scope.$watch('dplist.selected', function (newVal, oldVal) {
		if (newVal && newVal.id == 0) {
			$scope.param.data.parameter = 0;
			$scope.param.data.lowest = 0;
			$scope.noDiscount = true;
		} else {
			$scope.noDiscount = false;
		}
	});
});